<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/14
 * Time: 14:12
 */

namespace app\admin\controller;

use app\admin\model\Kami as km;
use think\Cache;

class Kami extends Common
{
    public function index()
    {
        Cache::store('redis')->set('dl_list', km::daili_type('s'));
        Cache::store('redis')->set('ksList', km::order('km_id desc')->paginate(10));
        $this->assign('dlarr', Cache::get('dl_list'));
        $this->assign('kmarr', Cache::get('ksList'));

        return view();
    }

    public function kami()
    {
        if (request()->isAjax()) {
            $data = input('');
            $data['number'] ? $number = $data['number'] : $number = 1;

            if ($data['km_type'] == 0) {
                $this->error('请选择卡密类型');
            }
            $data['km_time'] = time();
            $data['km_sell'] = 0;
            if ($data['km_uid'] == false) {
                $this->error('请输入用户名');
            }
            $userRow = km::user_row($data['km_uid']);
            if (!$userRow) {
                $this->error('用户不存在');
            }
            $data['km_uid'] = $userRow['id'];
            unset($data['number']);

            for ($i = 0; $i < $number; $i++) {
                $data['km_number'] = md5(randCode('16', 1,$data['kaminums']));
                unset($data['kaminums']);
                $row = db('kami')->insert($data);
            }
            if ($row) {
                $this->success('卡密生成成功');
            } else {
                $this->error('卡密生成失败');
            }

        }
    }

    public function Del_km($km_id){
        if(request()->isAjax()){
            if(km::where('km_id','in',$km_id)->delete()){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }
}